package com.fp.m7.client.presenter;

import java.util.Date;

import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.PlaceRequest;
import net.customware.gwt.presenter.client.place.PlaceRequestEvent;
import net.customware.gwt.presenter.client.place.TokenFormatter;
import net.customware.gwt.presenter.client.widget.WidgetDisplay;
import net.customware.gwt.presenter.client.widget.WidgetPresenter;

import com.allen_sauer.gwt.log.client.Log;
import com.fp.m7.client.AccountContext;
import com.fp.m7.client.MyMessages;
import com.fp.m7.client.event.InfoEvent;
import com.fp.m7.client.place.ProfilePlace;
import com.fp.m7.shared.dto.CommentDto;
import com.fp.m7.shared.services.CommentServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;

/**
 * A presenter for a single posted comment.
 * 
 * @author 7
 */
public class CommentPresenter extends WidgetPresenter<CommentPresenter.Display> {

    public interface Display extends WidgetDisplay {

        public void setContents(String contents, Date date, String authorNickname, String avatarUrl, String profileToken);

        public void showDeleteButton(boolean mode);

        public HasClickHandlers getProfileButton();

        public HasClickHandlers getDeleteButton();

    }
    
    private static MyMessages msg = GWT.create(MyMessages.class);

    private CommentDto commentDto;

    private AccountContext accountContext;
    private CommentServiceAsync commentService;
    private TokenFormatter tokenFormatter;

    @Inject
    public CommentPresenter(Display display, EventBus eventBus, TokenFormatter tokenFormatter, //
            AccountContext accountContext, CommentServiceAsync commentService) {
        super(display, eventBus);

        this.tokenFormatter = tokenFormatter;
        this.accountContext = accountContext;
        this.commentService = commentService;
    }

    @Override
    protected void onBind() {
        display.getProfileButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent evt) {
                if (commentDto != null) {
                    PlaceRequest req = new PlaceRequest(ProfilePlace.NAME) //
                            .with(ProfilePlace.NICKNAME, commentDto.getUserDto().getNickname());
                    eventBus.fireEvent(new PlaceRequestEvent(req));
                }
            }
        });

        display.getDeleteButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent evt) {
                if (authUserIsAuthorOfThisComment()) {
                    deleteComment();
                }
            }
        });
    }

    @Override
    protected void onUnbind() {
    }

    @Override
    protected void onRevealDisplay() {
    }

    private void deleteComment() {
        commentService.deleteComment(commentDto, new AsyncCallback<Boolean>() {
            @Override
            public void onSuccess(Boolean arg0) {
                eventBus.fireEvent(new InfoEvent(msg.commentDeleted()));
                display.asWidget().removeFromParent();
                unbind();
            }

            @Override
            public void onFailure(Throwable throwable) {
                Log.info("Error while deleting a comment", throwable);
            }
        });
    }

    public void setCommentDto(CommentDto commentDto) {
        this.commentDto = commentDto;

        if (commentDto != null) {
            PlaceRequest req = new PlaceRequest(ProfilePlace.NAME) //
                    .with(ProfilePlace.NICKNAME, commentDto.getUserDto().getNickname());

            display.setContents(commentDto.getContent(), //
                    commentDto.getDate(), //
                    commentDto.getUserDto().getNickname(), //
                    commentDto.getUserDto().getAvatarUrl(), //
                    tokenFormatter.toHistoryToken(req));
        }

        display.showDeleteButton(authUserIsAuthorOfThisComment());
    }

    private boolean authUserIsAuthorOfThisComment() {
        if (commentDto != null && accountContext.getAuthenticatedAccount() != null
                && accountContext.getAuthenticatedAccount().getUserDto().equals(commentDto.getUserDto())) {
            return true;
        }
        return false;
    }

}
